package com.ts.base.config;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@RestControllerAdvice(basePackages = "com.ts.base.controller")
public class CustomResponseAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {

        System.out.println("In supports() methods of "+getClass().getSimpleName());
        //在这里如果 需要兼顾以往接口的话 可以添加新的注解 通过注解确认是否 需要添加APIResult的反馈
        //return methodParameter.getMethod().isAnnotationPresent(APIResultAnnotation.class);
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        System.out.println("In beforeBodyWrite() method of "+getClass().getSimpleName());

        if(o instanceof APIResult){
            return o;
        }

        return APIResult.success(o);

    }
}
